<template>
  <el-dialog
    title="修改微信用户参数"
    :close-on-click-modal="false"
    :visible.sync="visible"
    class="missionPart"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <div class="tablePart">
        <el-form-item label="管理后台用户名:" prop="sysUserId">
          <el-select
            v-model="dataForm.sysUserId"
            placeholder="请选择管理后台绑定用户"
            clearable
            filterable
          >
            <el-option
              v-for="(item, index) in userOptions"
              :key="index"
              :label="item.username"
              :value="item.userId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="用户名:" prop="username">
          <el-input v-model="dataForm.username" placeholder="用户名" />
        </el-form-item>
        <el-form-item label="电话号码:" prop="tel">
          <el-input v-model="dataForm.tel" placeholder="电话号码" />
        </el-form-item>
        <el-form-item label="用户激活状态状态:" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio :label="0">正常</el-radio>
            <el-radio :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否接收警报:" prop="isAlarm">
          <el-radio-group v-model="dataForm.isAlarm">
            <el-radio :label="1">接收</el-radio>
            <el-radio :label="0">不接收</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="头像url:" prop="avatar" class="fullList">
          <img :src="dataForm.avatar" alt="dataForm.avatar" />
        </el-form-item>
      </div>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        avatar: null,
        createTime: null,
        createUser: null,
        id: 0,
        openId: null,
        status: 0,
        sysUserId: 0,
        tel: null,
        isAlarm: null,
        updateTime: null,
        username: null
      },
      userOptions: [],
      dataRule: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    init(id) {
      this.getUserList();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/cms/tbUserInfo/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm = data.tbUserInfo;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/cms/tbUserInfo/update`),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              username: this.dataForm.username,
              name: this.dataForm.name,
              status: this.dataForm.status,
              sysUserId: this.dataForm.sysUserId,
              isAlarm: this.dataForm.isAlarm,
              tel: this.dataForm.tel
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    // 获取用户列表
    getUserList() {
      this.$http({
        url: this.$http.adornUrl(`/sys/user/combo`),
        method: "get",
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.userOptions = data.list;
        }
      });
    }
  }
};
</script>
